﻿Imports System.Globalization
Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Data.SqlClient
Imports ADOConnection

Public Module GeneralParameters
    'Friend fCallForm As Form

    'Params Connect
    Friend connectString As String
    Friend cnnStrSy As String
    Friend strWhereDMKhoByUser As String
    Friend strLoginName_SA As String = "sa"
    Friend strPassword_SA As String = ""


    'Cac bien filter cua LocPhieu cua tblSysVar
    Friend myNX As String
    Friend myLoaiMH As String
    Friend myLK As String
    Friend mySoPhieu As String
    Friend myDVGN As String
    Friend myTCNX As String

    ''' <summary>
    ''' Tinh trang phieu KH, TCT
    ''' </summary>
    ''' <remarks></remarks>
    Friend myBCTH As Integer

    Friend myHopDong As Integer
    Friend myKyQT As Integer
    Friend myNGAY As Date
    Friend myNGAYTo As Date
    Friend mySqlDK As String    'Dieu kien bang cau query cua Sql
    Friend myShowDK As String   'Dieu kien bang cau hien thi tuong minh

    Public nonNumberEntered As Boolean 'Bien dung cho event textbox_Keydown, textbox_Keyup
    Public dsQuyenTrenForm As New ArrayList() 'bien dung de luu tru cac quyen tren form Main 
End Module

Public Module GeneralCode
    Public Enum enumDOITUONG
        NONE = 0
        BAN = 1
        DONVI = 2
        KHO = 3
        LOAIKHO = 4
        TINHCHATNHAP = 5
        TINHCHATXUAT = 6
        THANHTOAN = 7
        CCL = 8
        DVT = 9
    End Enum

    ''' <summary>
    ''' 2014-02-17
    ''' HT Code: Thêm tình trạng phiếu cho biết phiếu đang cập nhật ở giai đoạn nào
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum enumTINHTRANGPHIEU
        NONE = 0 'Phiếu mới được kế hoạch lập nhưng chưa duyệt
        KEHOACH = 1 'Kế hoạch duyệt phiếu
        KHO = 2 'Kho duyệt phiếu
        TAICHINHSL = 3 'Tài chính SL duyệt phiếu
        TAICHINHTIEN = 4 'Tài chính tiền duyệt phiếu
    End Enum

    ''' <summary>
    ''' enum Phân quyền sử dụng
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum enumPHANQUYEN
        'NONE = 0
        KEHOACH = 1
        TCT = 2
        CHUNHIEMBAN = 3
        TCSL = 4
        KHO = 5
    End Enum

    Public Enum enumTCNX
        NONE = 0
        DCSS = 1
        NB = 2
        DCT = 3
    End Enum

    ''' <summary>
    ''' Dùng cho log
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum enumAction
        ThemMoi = 1
        Sua = 2
        Xoa = 3
        DangNhap = 4
        DangXuat = 5
        XacNhan = 6
        HuyXacNhan = 7
    End Enum
    '''' <summary>
    '''' Lọc ban ID theo phân quyền
    '''' Chú ý: Hiện tại phân quyền giữa user và ban là khác nhau
    '''' </summary>
    '''' <param name="loai"></param>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Public Function LocPhanQuyen(ByVal loai As enumPHANQUYEN) As Integer
    '    Dim ban_id As Integer = -1
    '    Select Case loai
    '        Case enumPHANQUYEN.KEHOACH
    '            ban_id = 1
    '        Case enumPHANQUYEN.TCT
    '            ban_id = 2
    '        Case enumPHANQUYEN.CHUNHIEMBAN
    '            ban_id = 3
    '        Case enumPHANQUYEN.TCSL
    '            ban_id = 4
    '        Case enumPHANQUYEN.KHO
    '            ban_id = 5
    '    End Select
    '    Return ban_id
    'End Function

    Public Function LocNhapXuat(ByVal loai As enumDOITUONG) As String
        Dim strLoai As String
        Select Case loai
            Case enumDoiTuong.TINHCHATNHAP
                strLoai = "N"
            Case enumDoiTuong.TINHCHATXUAT
                strLoai = "X"
            Case Else
                strLoai = ""
        End Select
        Return strLoai
    End Function

    Public Function LocDoiTuong(Optional ByVal loai As enumDoiTuong = enumDoiTuong.NONE) As String
        Dim strLoai As String
        Select Case loai
            Case enumDOITUONG.DONVI
                strLoai = "D"
            Case enumDoiTuong.KHO
                strLoai = "K"
            Case enumDoiTuong.LOAIKHO
                strLoai = "L"
            Case enumDoiTuong.TINHCHATNHAP
                strLoai = "N"
            Case enumDOITUONG.TINHCHATXUAT
                strLoai = "X"
            Case enumDOITUONG.THANHTOAN
                strLoai = "M"
            Case enumDOITUONG.CCL
                strLoai = "H"
            Case enumDOITUONG.DVT
                strLoai = "U"
            Case Else
                strLoai = ""
        End Select
        Return strLoai
    End Function

    Public Function LocTCNX(ByVal loai As enumTCNX) As String
        Dim strLoai As String
        Select Case loai
            Case enumTCNX.DCSS
                strLoai = "DCSS"
            Case enumTCNX.NB
                strLoai = "NB"
            Case enumTCNX.DCT
                strLoai = "DCT"
            Case Else
                strLoai = ""
        End Select
        Return strLoai
    End Function

    Public Sub SetColorControl(ByRef control As Object, ByVal bool As Boolean)
        If bool Then
            'control.ForeColor = Color.Black
            control.BackColor = Color.FromArgb(192, 255, 192)
        Else
            'control.ForeColor = control.ForeColr
            control.BackColor = Color.White
        End If
    End Sub

    Public Sub SetColorAllControls(ByRef control As Object, ByVal bool As Boolean)
        SetColorControl(control, bool)
    End Sub

    Public Sub textbox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        nonNumberEntered = False
        If (e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9) Then
            If (e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9) Then
                If e.KeyCode <> Keys.Back Then
                    nonNumberEntered = True
                End If
            End If
        End If
    End Sub

    ' This event occurs after the KeyDown event and can be used 
    ' to prevent characters from entering the control.
    Public Sub textbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        ' Check for the flag being set in the KeyDown event.
        If nonNumberEntered = True Then
            ' Stop the character from being entered into the control since it is non-numerical.
            e.Handled = True
        End If
    End Sub 'textBox1_KeyPress

    'Public Function GetLevel(ByVal tvNode As TreeNode) As Integer
    '    Dim level As Integer = 0
    '    'Xac dinh level Co Cau duoc chon
    '    While Not tvNode Is Nothing
    '        level = level + 1
    '        tvNode = tvNode.Parent
    '    End While
    '    Return level
    'End Function

    Public Sub ControlSetFocus(ByVal control As Control)
        ' Set focus to the control, if it can receive focus.
        If control.CanFocus Then
            control.Focus()
        End If
    End Sub

    Public Function FormatMoneyStyle() As String
        Return "#,##0.##"
    End Function

    Public Function FormatQuantityStyle() As String
        Return "#,##0.##"
    End Function

    Public Function FormatDateStyle() As String
        Return "dd/MM/yyyy"
    End Function

    Public Sub ShowError(ByVal sErr As String)
        MessageBox.Show(sErr, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Public Sub ShowMessages(ByVal sMsg As String)
        MessageBox.Show(sMsg, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Public Function GetDateParam(ByVal pDate As Date) As Object
        If pDate = "1/1/2001" Then
            Return DBNull.Value
        End If
        Return pDate
    End Function

    Public Function DateNull() As Date
        Return "1/1/2001"
    End Function

    'Chuyển kiểu String của ngày có dạng FormatDateStyle() sang thành kiểu Date
    'Cách chuyển này sẽ không phụ thuộc vào ngày hệ thống như cách ép kiểu str(1) + str(0) + str(2)
    Public Function ConvertStringTypeToDateType(ByVal strNgay As String) As Date
        Dim dt As New DateTimePicker
        dt.Format = DateTimePickerFormat.Custom
        dt.CustomFormat = FormatDateStyle()
        dt.Text = strNgay
        Return dt.Value
    End Function

    'Chuyen dtNgay co kieu Date sang kieu String
    'Cách này sẽ chuyển đúng kiểu ngày dạng String theo đúng định dạng cần chuyển ("M/d/yyyy") mà không phụ thuộc kiểu ngày hệ thống
    'Nếu chuyển theo kiểu dtNgay.ToString() thì sẽ chuyển theo định dạng ngày hệ thống
    Public Function ConvertDateTypeToStringType(ByVal dtNgay As Date) As String
        Return Format(dtNgay, "M/d/yyyy")
    End Function

    Public Function KiemTraNgayHopLe(ByRef strDate As String) As Boolean
        Try
            If strDate <> "  /  /" Then
                Dim dt As New DateTimePicker
                dt.Format = DateTimePickerFormat.Custom
                dt.CustomFormat = FormatDateStyle()
                dt.Text = strDate
                Dim dtValue As Date = dt.Value
                'Neu dung kieu date thi chuyen lai du lieu text
                Dim str(2) As String
                str = dtValue.ToShortDateString.Split("/")
                If CInt(str(0)) < 10 Then
                    str(0) = "0" + CInt(str(0)).ToString()
                End If
                If CInt(str(1)) < 10 Then
                    str(1) = "0" + CInt(str(1)).ToString()
                End If
                strDate = str(0) + "/" + str(1) + "/" + str(2)
            Else
                Return True
            End If
        Catch ex As Exception
            strDate = ""
            Return False
        End Try
        Return True
    End Function

    'Thu tuc nay dung de kiem tra bat dau tu 1/1 la se chuyen doi du lieu dau nam
    Public Function KiemTraChuyenDuLieu() As Boolean
        Dim ngay As Date
        ngay = CDate("1/1/" + Date.Now.Year.ToString())
        Dim objReader As SqlDataReader = Nothing
        Try
            Dim strSQL As String = "select * from tblNGAYDAUKY where kychuyen ='" + ngay.ToShortDateString + "'"
            objReader = BaseDB.ExecSql_DataReader(strSQL)
            'Neu da chuyen roi
            If objReader.Read() Then
                objReader.Close()
                Return True
            Else
                Dim str As String
                str = String.Format("Bắt đầu sang ngày {0}/{1}/{2} bạn phải thực hiện chuyển dữ liệu trước khi thực hiện thêm phiếu mới hoặc kiểm kê đầu năm. Bạn muốn thực hiện ngay lúc này?", ngay.Day.ToString(), ngay.Month.ToString(), ngay.Year.ToString())
                If MessageBox.Show(str, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    Dim frm As New frmChuyenDuLieuKy
                    frm.ShowDialog()
                    Return True
                End If
            End If
        Catch ex As Exception
            objReader.Close()
            Return False
            Throw ex
        End Try
        Return False
    End Function

#Region "DoiSoRaChu"

    Public Function docchu(ByVal so1 As Integer) As String

        Dim tram As Integer, chuc As Integer, dv As Integer, so As Integer
        Dim chu(0 To 9) As String, chuoi1, chuoi2, chuoi
        so = so1
        chu(0) = "không"
        chu(1) = "một"
        chu(2) = "hai"
        chu(3) = "ba"
        chu(4) = "bốn"
        chu(5) = "năm"
        chu(6) = "sáu"
        chu(7) = "bảy"
        chu(8) = "tám"
        chu(9) = "chín"
        tram = so \ 100
        so = so Mod 100
        chuc = so \ 10
        dv = so Mod 10
        Select Case chuc
            Case 0
                chuoi2 = IIf(tram = 0, "", IIf(dv = 0, "", "lẻ"))
            Case 1
                chuoi2 = "mười"
            Case Else
                chuoi2 = chu(chuc) + " mươi"
        End Select
        Select Case tram
            Case 0
                chuoi1 = ""
            Case Else
                chuoi1 = chu(tram) + " trăm "
        End Select
        Select Case dv
            Case 0
                chuoi = ""
            Case 1
                chuoi = IIf((chuoi2 = "") Or (chuoi2 = "mười") Or (chuoi2 = "lẻ"), "một", "mốt")
            Case 5
                chuoi = IIf((chuoi2 = "lẻ") Or (chuoi2 = ""), "năm", "lăm")
            Case Else
                chuoi = chu(dv)
        End Select

        docchu = Trim(chuoi1 + IIf(chuoi2 <> "", chuoi2, "") + " " + chuoi)
    End Function

    Public Function doc(ByVal so1 As Double) As String
        Dim chuoi1, chuoi2, chuoi3, chuoi4 As String
        chuoi1 = ""
        If so1 = 0 Then
            doc = "không"
            Exit Function
        End If
        Dim so As Double
        so = so1
        Dim ty As Integer, trieu As Integer, ngan As Integer, dv As Integer, rr As Double
        ty = Int(so / 1000000000)
        rr = ty
        so = so - CDbl(1000000000) * CDbl(ty)
        trieu = so \ 1000000
        so = so Mod 1000000
        ngan = so \ 1000
        dv = so Mod 1000
        If ty > 1000 Then
            chuoi1 = doc(rr) + " tỷ"
        ElseIf ty > 0 Then
            chuoi1 = docchu(ty) + " tỷ"
        End If
        chuoi2 = IIf(trieu > 0, docchu(trieu) + " triệu", "")
        chuoi3 = IIf(ngan > 0, docchu(ngan) + " ngàn", "")
        chuoi4 = IIf(dv > 0, docchu(dv), "")
        doc = Trim(chuoi1 + IIf(chuoi2 <> "", " " + chuoi2, "") + IIf(chuoi3 <> "", " " + chuoi3, "") + IIf(chuoi4 <> "", " " + chuoi4, ""))
    End Function

    Public Function DoiSoRaChu(ByVal so As Double)
        Dim so1 As Double, le As String, chuoi As String
        chuoi = Format(so, "000000000000000000.00")
        so1 = Val(Left(chuoi, Len(chuoi) - 3))
        le = Right(chuoi, 2)
        If Val(le) > 0 Then
            If Val(le) < 10 Then
                chuoi = doc(so1) + " phẩy không " + doc(Val(le))
            Else
                chuoi = doc(so1) + " phẩy " + doc(Val(le))
            End If
        Else
            chuoi = doc(so1)
        End If
            If (chuoi.Length <> 0) Then
                Dim c As String
                c = chuoi.Substring(0, 1)
                chuoi = c.ToUpper() + chuoi.Substring(1)
            End If
        DoiSoRaChu = chuoi + " đồng"
    End Function


    'Public Function DoiSoRaChu(ByVal so As Double) As String
    '    Dim ketqua As String = ""
    '    Dim ty, duoity As Double
    '    ty = Math.Floor(so / 1000000000)
    '    duoity = Math.Floor(so Mod 1000000000)
    '    While (ty > 0)
    '        ketqua &= DoiSoRaChu(ty) + " tỷ "
    '        ty = Math.Floor(ty / 1000000000)
    '    End While
    '    ketqua &= DocHangTrieu(duoity)
    '    Return ketqua
    'End Function

    'Public Function DocHangTrieu(ByVal so As Double) As String
    '    Dim ketqua As String = ""
    '    Dim trieu, ngan, tram As String
    '    trieu = DocHangTram(Math.Floor(so / 1000000))
    '    ngan = DocHangTram(Math.Floor((so Mod 1000000) / 1000))
    '    tram = DocHangTram(Math.Floor(so Mod 1000))
    '    If (trieu.Trim <> "") Then
    '        ketqua &= trieu + " triệu "
    '    End If
    '    If (ngan.Trim <> "") Then
    '        ketqua &= ngan + " ngàn "
    '    End If
    '    If (tram.Trim <> "") Then
    '        ketqua &= tram
    '    End If
    '    Return ketqua
    'End Function

    'Public Function DocHangTram(ByVal so As Integer) As String
    '    Dim tram, chuc, dv As Integer
    '    tram = Math.Floor(so / 100)
    '    chuc = Math.Floor((so Mod 100) / 10)
    '    dv = so Mod 10
    '    Dim ketqua As String = ""
    '    If (tram > 0) Then
    '        ketqua &= DocMotSo(tram) + " trăm "
    '    End If

    '    If (chuc > 0) Then
    '        If (chuc > 1) Then
    '            ketqua &= DocMotSo(chuc) + " mươi "
    '        Else
    '            ketqua &= " mười "
    '        End If
    '    End If

    '    If (dv > 0) Then
    '        If (tram > 0) And (chuc = 0) Then
    '            ketqua &= "lẻ " + DocMotSo(dv)
    '        Else
    '            ketqua &= DocMotSo(dv)
    '        End If
    '    End If
    '    Return ketqua
    'End Function

    'Public Function DocMotSo(ByVal so As Double) As String
    '    Dim ketqua As String = ""
    '    Select Case so
    '        Case 0 : ketqua = ""
    '        Case 1 : ketqua = "một"
    '        Case 2 : ketqua = "hai"
    '        Case 3 : ketqua = "ba"
    '        Case 4 : ketqua = "bốn"
    '        Case 5 : ketqua = "năm"
    '        Case 6 : ketqua = "sáu"
    '        Case 7 : ketqua = "bảy"
    '        Case 8 : ketqua = "tám"
    '        Case 9 : ketqua = "chín"
    '    End Select
    '    Return ketqua
    'End Function
#End Region

    Public Function ChuanHoaChuoi(ByVal obj As Object) As String
        If obj Is DBNull.Value Then
            Return ""
        End If
        Return obj.ToString
    End Function

    Public Function ChuanHoaSo(ByVal num As Object) As Double
        If num Is DBNull.Value Then
            Return 0
        End If
        Return num
    End Function

    Public Function IsSoChan(ByVal pValue As Double) As Boolean
        Dim Kq As Boolean = False
        Dim strValue As String = pValue.ToString("000000000000000.00")

        Dim strPhanNguyen As String = strValue.Substring(0, 15)
        Dim strPhanThapPhan As String = strValue.Substring(16, 2)

        If Val(strPhanThapPhan) = 0 Then
            Kq = True
        End If

        Return Kq
    End Function
End Module
